{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Pull Portfolio Performance and Factor Risk Data\n",
    "\n",
    "This tutorial uses a pre-populated portfolio (accessible to all internal users) and walks through pulling performance and factor risk data *(for demo, not client distribution, purposes)*."
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 1: Authenticate and Initialize Your Session"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n",
    "import datetime as dt\n",
    "\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.markets.report import PerformanceReport, FactorRiskReport\n",
    "from gs_quant.session import GsSession, Environment\n",
    "\n",
    "GsSession.use(Environment.PROD)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 2: Get all Portfolio Reports\n",
    "\n",
    "The `PortfolioManager` class allows for easy retrieval and scheduling of portfolio reports. Simply running:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "all_reports = PortfolioManager('MPZV9A0F1EMQGG79').get_reports()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "will return a list of `Report` objects that represent the reports associated with the portfolio.\n",
    "\n",
    "## Step 3: Find Portfolio Performance Analytics Report and Pull Data\n",
    "\n",
    "The GS Quant `Report` class is inherited by report subclasses, like `FactorRiskReport` and `PerformanceReport`, each of which corresponds to a type of Marquee report. Each subclass then has additional functions specific to its report type. In this case, we'd like to find the `PerformanceReport` associated with this portfolio, and leverage its functions to retrieve data. In this example, we will pull all historical PnL, gross exposure, and net exposure available.\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "performance_report = list(filter(lambda report: isinstance(report, PerformanceReport), all_reports))[0]\n",
    "pnl = performance_report.get_pnl(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "gross_exposure = performance_report.get_gross_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "net_exposure = performance_report.get_net_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "\n",
    "print(f'PnL: \\n{pnl.__str__()}')\n",
    "print(f'Gross Exposure: \\n{gross_exposure.__str__()}')\n",
    "print(f'Net Exposure: \\n{net_exposure.__str__()}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 4: Find Factor Risk Report and Pull Data\n",
    "Now let's find a factor risk report and pull its correlating `FactorRiskReport`."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "risk_model_id = 'BARRA_USSLOWL'\n",
    "factor_risk_reports = list(filter(lambda report: isinstance(report, FactorRiskReport) and report.get_risk_model_id() == risk_model_id, all_reports))\n",
    "if len(factor_risk_reports) == 0:\n",
    "    print(f'This portfolio does not have a factor risk report with the risk model {risk_model_id}. Please create a new factor risk report and schedule it before proceeding.')\n",
    "factor_risk_report = factor_risk_reports[0]\n",
    "\n",
    "print(f'Factor risk model found with ID: \"{factor_risk_report.id}\".')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now that we have our factor risk report, we can leverage the functionality of the `FactorRiskReport` class to pull attribution and risk data. In this example, let's pull historical data on factor, specific, and total PnL for the first five months of 2021:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "factor_pnl = factor_risk_report.get_factor_pnl(factor_name='Factor', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "specific_pnl = factor_risk_report.get_factor_pnl(factor_name='Specific', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "total_pnl = factor_risk_report.get_factor_pnl(factor_name='Total', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "\n",
    "print(f'Factor: \\n{factor_pnl.__str__()}')\n",
    "print(f'Specific: \\n{specific_pnl.__str__()}')\n",
    "print(f'Total: \\n{total_pnl.__str__()}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now let's pull the breakdown of proportion of risk among the different factor types over time:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "market_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Market', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "style_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Style', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "industry_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Industry', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "country_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Country', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))\n",
    "\n",
    "print(f'Market: \\n{market_prop_of_risk.__str__()}')\n",
    "print(f'Style: \\n{style_prop_of_risk.__str__()}')\n",
    "print(f'Industry: \\n{industry_prop_of_risk.__str__()}')\n",
    "print(f'Country: \\n{country_prop_of_risk.__str__()}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "### You're all set, Congrats! What's next?\n",
    "\n",
    "* [Creating and scheduling a new factor risk report](../examples/marquee/00_create_factor_risk_report.ipynb)\n",
    "\n",
    "* [Updating the portfolio with new positions](../tutorials/Update%20Historical%20Portfolio.ipynb)\n",
    "\n",
    "* [Retrieving the portfolio's performance analytics](../tutorials/Pull%20Portfolio%20Performance%20Data.ipynb)\n",
    "\n",
    "\n",
    "*Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}